package org.xy.sys.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xy.common.page.PageManager;
import org.xy.sys.domain.ModelInfo;
import org.xy.sys.domain.SqlInfo;
import org.xy.sys.service.ModelService;
import org.xy.sys.service.SqlService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试
 *
 * @author liubq
 */
@Controller
@RequestMapping(value = "/sql/")
public class SqlController {

    @Resource(name = "sqlService")
    private SqlService sqlService;
    @Resource(name = "modelService")
    private ModelService modelService;

    @RequestMapping("/loadForm.action")
    public String loadForm(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("id", request.getParameter("id"));
        String typeId = request.getParameter("typeId");
        ModelInfo cond = new ModelInfo();
        cond.setTypeId(Long.valueOf(typeId));
        cond.setOrderBy("t.id desc ");
        List<ModelInfo> dataList = modelService.queryAll(cond);
        request.setAttribute("modelDataList", dataList);
        return "sql/sqlForm";
    }

    @RequestMapping("/manage.action")
    public String manage(HttpServletRequest request, HttpServletResponse response) {
        return "sql/sqlList";
    }


    @RequestMapping("/manageList.action")
    @ResponseBody
    public PageManager manageList(SqlInfo cond, PageManager page) {
        cond.setOrderBy("t.seq_num asc,t.id desc");
        PageManager resPage = sqlService.queryByPage(cond, page);
        return resPage;
    }


    @RequestMapping("/save.action")
    @ResponseBody
    public Map<String, Object> save(SqlInfo cond) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            if (cond.getId() == null || cond.getId() <= 0) {
                sqlService.insertData(cond);
            } else {
                sqlService.updateData(cond);
            }
            map.put("success", "true");
            map.put("id", cond.getId());
        } catch (Exception e) {
            map.put("success", "false");
            map.put("message", e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/get.action")
    @ResponseBody
    public SqlInfo get(SqlInfo cond) {
        SqlInfo data = sqlService.query(cond);
        return data;
    }

    @RequestMapping("/delete.action")
    @ResponseBody
    public Map<String, Object> delete(SqlInfo cond) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            sqlService.deleteData(cond);
            map.put("success", "true");
            map.put("id", cond.getId());
        } catch (Exception e) {
            map.put("success", "false");
            map.put("message", e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

}
